package com.quanyan.riding.web;

import com.github.ltsopensource.core.json.JSON;
import com.quanyan.api.APIResponse;
import com.quanyan.common.utils.StringUtil;
import com.quanyan.riding.entity.db.TbRidingBikePeople;
import com.quanyan.riding.entity.vo.req.ReqJoinRidingBike;
import com.quanyan.riding.entity.vo.req.ReqUpdateBikePeopleLocation;
import com.quanyan.riding.entity.vo.resp.RespRidingBikeManager;
import com.quanyan.riding.entity.vo.resp.RespRidingCheckBike;
import com.quanyan.riding.entity.vo.resp.RespUpdateBikePeopleLocation;
import com.quanyan.riding.service.RidingBikeService;
import com.quanyan.riding.util.RunRidingEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @author xian-guo
 * @email 498152332@qq.com
 * @date 2016/8/29.
 */
@RestController
@RequestMapping(value = "api/riding")
public class RidingBikeController {

    private static final Logger LOGGER = LoggerFactory.getLogger(RidingBikeController.class);

    /**
     * 车队成员
     */
    @Autowired
    private RidingBikeService ridingBikeService;

    /**
     *查询用户创建的和加入的车队
     * @param paramMap
     * @return
     */
    @RequestMapping(value = "ridingBikeMessage",method = RequestMethod.POST)
    public APIResponse<List<RespRidingCheckBike>> getRidingBikeMessage(@RequestBody(required = true) Map<String,Object> paramMap){
        LOGGER.debug("--------------->>>>"+JSON.toJSONString(paramMap));
        String userId= (String) paramMap.get("userId");
        if (StringUtil.isBlankOrNull(userId)){
            return APIResponse.returnFail(RunRidingEnum.PARAMETER_NOT_ALLOW_NULL);
        }
        return ridingBikeService.getRidingBikeMessage(userId);
    }

    /**
     * 创建车队
     * @param paramMap
     * @return
     */
    @RequestMapping(value = "createRidingBike",method = RequestMethod.POST)
    public APIResponse createRidingBike(@RequestBody(required = true) Map<String,Object> paramMap){
        LOGGER.debug("--------------->>>>"+JSON.toJSONString(paramMap));
        String userId= (String) paramMap.get("userId");
        String longLat= (String) paramMap.get("longLat");
        if (StringUtil.isBlankOrNull(userId) || StringUtil.isBlankOrNull(longLat)){
            return APIResponse.returnFail(RunRidingEnum.PARAMETER_NOT_ALLOW_NULL);
        }
        return ridingBikeService.createRidingBike(userId,longLat);
    }

    /**
     * 加入车队
     * @param reqJoinRidingBike
     * @return
     */
    @RequestMapping(value = "joinRidingBike",method = RequestMethod.POST)
    public APIResponse<List<TbRidingBikePeople>> joinRidingBike(@RequestBody(required = true)ReqJoinRidingBike reqJoinRidingBike){
        LOGGER.debug("--------------->>>>"+JSON.toJSONString(reqJoinRidingBike));
        if (StringUtil.isBlankOrNull(reqJoinRidingBike.getUserId()) || StringUtil.isBlankOrNull(reqJoinRidingBike.getBikeCommand()) || StringUtil.isBlankOrNull(reqJoinRidingBike.getLongLat())){
            return APIResponse.returnFail(RunRidingEnum.PARAMETER_NOT_ALLOW_NULL);
        }
        return ridingBikeService.joinRidingBike(reqJoinRidingBike);
    }

    /**
     * 用户创建车队成功，点击进入车队
     * @param paramMap
     * @return
     */
    @RequestMapping(value = "createBikeJoin",method = RequestMethod.POST)
    public APIResponse<List<TbRidingBikePeople>> createBikeJoin(@RequestBody(required = true)Map<String,Object> paramMap){
        LOGGER.debug("--------------->>>>"+JSON.toJSONString(paramMap));
        String command= (String) paramMap.get("command");
        if (StringUtil.isBlankOrNull(command)){
            return APIResponse.returnFail(RunRidingEnum.PARAMETER_NOT_ALLOW_NULL);
        }
        return ridingBikeService.createBikeJoin(command);
    }

    /**
     * 实时跟新车队成员位置
     * @param updateBikePeopleLocation
     * @return
     */
    @RequestMapping(value = "updateBikePeopleLocation",method = RequestMethod.POST)
    public APIResponse<List<RespUpdateBikePeopleLocation>> updateRidingBikePeopleLocation(@RequestBody(required = true)ReqUpdateBikePeopleLocation updateBikePeopleLocation){
        LOGGER.debug("--------------->>>>"+JSON.toJSONString(updateBikePeopleLocation));
        if (StringUtil.isBlankOrNull(updateBikePeopleLocation.getUserId()) || StringUtil.isBlankOrNull(updateBikePeopleLocation.getLongLat()) || StringUtil.isBlankOrNull(updateBikePeopleLocation.getCommand())){
            return APIResponse.returnFail(RunRidingEnum.PARAMETER_NOT_ALLOW_NULL);
        }
        return ridingBikeService.updateRidingBikePeopleLocation(updateBikePeopleLocation);
    }

    /**
     * 车队管理
     * @param paramMap
     * @return
     */
    @RequestMapping(value = "managerBikePerple",method = RequestMethod.POST)
    public APIResponse<RespRidingBikeManager> managerBikePerple(@RequestBody(required = true)Map<String,Object> paramMap){
        LOGGER.debug("--------------->>>>"+JSON.toJSONString(paramMap));
        String command = (String) paramMap.get("command");
        if (StringUtil.isBlankOrNull(command)){
            return APIResponse.returnFail(RunRidingEnum.PARAMETER_NOT_ALLOW_NULL);
        }
        return ridingBikeService.managerBikePerple(command);
    }

    /**
     * 退出车队
     * @param paramMap
     * @return
     */
    @RequestMapping(value = "quitRidingBike",method = RequestMethod.POST)
    public APIResponse quitRidingBike(@RequestBody(required = true)Map<String,Object> paramMap){
        LOGGER.debug("--------------->>>>"+JSON.toJSONString(paramMap));
        String userId = (String) paramMap.get("userId");
        String command = (String) paramMap.get("command");
        if (StringUtil.isBlankOrNull(command) || StringUtil.isBlankOrNull(userId)){
            return APIResponse.returnFail(RunRidingEnum.PARAMETER_NOT_ALLOW_NULL);
        }
        return ridingBikeService.quitRidingBike(userId,command);
    }

    /**
     * 删除车队成员
     * @param paramMap
     * @return
     */
    @RequestMapping(value = "delRidingBikePeople",method = RequestMethod.POST)
    public APIResponse delRidingBikePeople(@RequestBody(required = true)Map<String,Object> paramMap){
        LOGGER.debug("--------------->>>>"+JSON.toJSONString(paramMap));
        String userId = (String) paramMap.get("userId");
        String command = (String) paramMap.get("command");
        String leaderId = (String) paramMap.get("leaderId");
        if (StringUtil.isBlankOrNull(command) || StringUtil.isBlankOrNull(userId) || StringUtil.isBlankOrNull(leaderId)){
            return APIResponse.returnFail(RunRidingEnum.PARAMETER_NOT_ALLOW_NULL);
        }
        return ridingBikeService.delRidingBikePeople(leaderId, userId,command);
    }

    /**
     * 修改车队公告
     * @param paramMap
     * @return
     */
    @RequestMapping(value = "updateRidingBikeNotice",method = RequestMethod.POST)
    public APIResponse updateRidingBikeNotice(@RequestBody(required = true)Map<String,Object> paramMap){
        LOGGER.debug("--------------->>>>"+JSON.toJSONString(paramMap));
        String userId = (String) paramMap.get("userId");
        String command = (String) paramMap.get("command");
        String notice = (String) paramMap.get("notice");
        if (StringUtil.isBlankOrNull(command) || StringUtil.isBlankOrNull(userId) || StringUtil.isBlankOrNull(notice)){
            return APIResponse.returnFail(RunRidingEnum.PARAMETER_NOT_ALLOW_NULL);
        }
        return ridingBikeService.updateRidingBikeNotice(userId,command,notice);
    }

}
